<template>
  <div class="help">
    <Navigator
      @searchResult="searchResult"
      :searchFather="searchFather"
    ></Navigator>
    <el-container style="margin-top: 60px">
      <el-aside class="sliderBar" :width="isCollapse ? '0px' : '240px'">
        <el-menu
          :default-active="router"
          class="el-menu-vertical-demo"
          @select="handleSelect"
          text-color="#000"
          active-text-color="#3FB549"
          :collapse="isCollapse"
        >
          <div class="title"></div>
          <!-- <el-menu-item
            :index="item.value"
            class="pl-30"
            v-for="(item, index) in menuList"
            :key="index"
          >
            <span slot="title">{{ item.name }}</span>
          </el-menu-item> -->
          <div class="input_box">
            <el-autocomplete
              class="inline-input"
              v-model="search"
              :fetch-suggestions="querySearch"
              placeholder="请输入搜索内容"
              :trigger-on-focus="false"
              @select="handleSearchSelect"
              @clear="handleClear"
              clearable
            >
              <i
                v-if="!search"
                slot="suffix"
                class="el-input__icon el-icon-search"
              ></i
            ></el-autocomplete>
            <!-- <el-input placeholder="请输入搜索关键字" v-model="search" @clear="handleClear">
              
            </el-input> -->
          </div>
          <template v-for="(item, index) in menuList">
            <el-submenu
              v-if="item.children !== undefined && item.children.length > 0"
              :index="item.title"
              :key="index"
            >
              <template #title>
                {{ item.title }}
              </template>
              <el-menu-item
                v-for="(items, idx) in item.children"
                :key="idx"
                :index="items.title"
                class="pl-50"
              >
                {{ items.title }}
              </el-menu-item>
            </el-submenu>
            <el-menu-item
              v-else
              :index="item.title"
              :key="item.title"
              class="pl-30"
            >
              {{ item.title }}
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>
      <el-main
        style="background: rgba(247, 250, 248, 1); transition: all 1s ease"
        :style="{ marginLeft: iscollapse(isCollapse) }"
      >
        <div style="padding: 14px 16px">
          <!-- <router-view></router-view> -->
          <el-tooltip
            class="item"
            effect="green"
            content="收起菜单"
            placement="right"
            v-if="!isCollapse"
            popper-class="sub-popper-class"
          >
            <i
              class="el-icon-s-fold"
              style="
                font-size: 28px;
                cursor: pointer;
                margin-bottom: 14px;
                color: rgba(63, 181, 73, 1);
              "
              @click="isOpen(true)"
            ></i>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="green"
            content="展开菜单"
            placement="right"
            popper-class="sub-popper-class"
            v-else
          >
            <i
              class="el-icon-s-unfold"
              style="
                font-size: 28px;
                cursor: pointer;
                margin-bottom: 14px;
                color: rgba(63, 181, 73, 1);
              "
              @click="isOpen(false)"
            ></i>
          </el-tooltip>

          <Introduction
            :title="routerTitle"
            @nowTitle="nowTitle"
          ></Introduction>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import Introduction from '@/components/introduction.vue'
import Navigator from '@/components/help-navigation.vue'

export default {
  name: 'MenuTree',
  components: {
    Introduction,
    Navigator,
  },
  head() {
    return {
      title: '帮助文档',
    }
  },
  data() {
    return {
      routerTitle: '',
      input: '',
      router: this.$cookies.get('RouterTitle') || '一、首页',
      // router: 'introduction',
      menuList: [],
      searchFather: '',
      search: '',
      isCollapse: false,
    }
  },
  $route: {
    handler: function (route) {
      this.router = this.$route.name
    },
    immediate: true,
  },
  async asyncData(ctx) {
    let menuList = await ctx.$axios.get(
      'http://localhost:3025/web/api/web_helpDocs/getRouterList'
    )
    return {
      menuList,
    }
  },
  created() {
    // console.log(this.$route.query.id,2222)
    // this.getData()
    if (!this.$route.query.id) {
      // console.log("没有id")
      this.router = '一、首页'
    }
  },
  destroyed() {
    this.$cookies.remove('RouterTitle')
  },
  methods: {
    isOpen(e) {
      console.log('e :>> ', e)
      e ? (this.isCollapse = true) : (this.isCollapse = false)
    },
    iscollapse(e) {
      return e ? '0px' : '238px'
    },
    getData() {
      this.$axios
        .get('http://localhost:3025/web/api/web_helpDocs/getRouterList')
        .then((res) => {
          // console.log('res :>> ', res)
          this.menuList = res
          console.log('this.menuList :>> ', this.menuList)
        })
    },
    nowTitle(val) {
      this.router = val
      // if(val){
      //   this.router = val;
      // }
    },
    handleSelect(key, keyPath) {
      this.$cookies.set('RouterTitle', key)
      if (key) {
        this.$axios.post('http://localhost:3025/web/api/searchRecords', {
          content: key,
          type: 2,
        })
      }
      this.router = key
      this.routerTitle = this.router
      this.searchFather = key
    },
    handleSearchSelect(e) {
      this.$cookies.set('RouterTitle', e.title)
      this.$axios
        .get('http://localhost:3025/web/api/web_helpDocs', {
          params: {
            filter: {
              where: {
                title: e.title,
              },
            },
          },
        })
        // getHelpData({
        //   where: {
        //     title: e.title,
        //   },
        // })
        .then((res) => {
          console.log(res, 33333)
          this.routerTitle = res[0].title
          this.router = res[0].title
          this.searchFather = res[0].title
        })
    },
    searchResult(value) {
      this.routerTitle = value[0].title
      this.router = value[0].title
      this.searchFather = value[0].title
    },
    handleClear() {
      document.activeElement.blur()
    },
    querySearch(queryString, cb) {
      if (queryString) {
        this.$axios.post('http://localhost:3025/web/api/searchRecords', {
          content: queryString,
          type: 1,
        })
      }
      this.$axios
        .get('http://localhost:3025/web/api/web_helpDocs/getSearchData', {
          params: {
            searchValue: queryString,
          },
        })
        // getSearchData({ searchValue: queryString })
        .then((res) => {
          console.log('res :>> ', res)
          let titleList = []
          res.forEach((element) => {
            var obj = {}
            obj.id = element.id
            obj.value = element.mdData
            obj.title = element.title
            titleList.push(obj)
          })
          cb(titleList)
        })
        .catch((err) => {
          console.log('err :>> ', err)
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.help {
  width: 100%;
  height: 100%;

  .sliderBar {
    position: fixed;
    left: 0;
    height: 100vh;
    transition: all 1s ease;

    // .el-menu-vertical-demo{
    //   background-color:var(--transparent-color-two) !important;
    // }
    .input_box {
      width: 240px;
      height: 60px;
      background: rgba(255, 255, 255, 1);
      display: flex;
      justify-content: center;

      ::v-deep .el-input__inner:hover,
      ::v-deep .el-input__inner:focus {
        // border-color: var(--background-color) !important;
        border-color: rgba(63, 181, 73, 1) !important;
      }

      ::v-deep .el-input__inner {
        border-radius: 2px !important;
        width: 80% !important;
      }

      ::v-deep .el-input {
        // border-color: var(--background-color) !important;
        padding: 8px 24px !important;
      }

      // ::v-deep .el-input .el-input--suffix {
      //   width: 160px  !important;
      //   height: 32px  !important;
      //   border-radius: 2px  !important;
      //   background: rgba(255, 255, 255, 1)  !important;
      //   border: 1px solid rgba(217, 217, 217, 1)
      // }

      ::v-deep .el-input__suffix {
        right: 27% !important;
      }
    }

    .el-menu {
      background-color: rgba(255, 255, 255, 1) !important;
      border-right: none !important;
      height: 100%;

      .title {
        margin: 10px;
      }

      .pl-10 {
        padding-left: 10px !important;
      }

      .pl-50 {
        padding-left: 40px !important;
        font-size: 13px;
      }
      ::v-deep .el-submenu .el-menu-item {
        color: rgba(0, 0, 0, 0.65) !important;
      }
      ::v-deep .el-submenu .el-menu-item.is-active {
        color: #3fb549 !important;
      }
      ::v-deep .el-menu-item {
        height: 48px !important;
        line-height: 46px !important;
        list-style: none !important;
        font-size: 16px !important;

        &:hover {
          // color: #0776f9 !important;
          // color: rgba(63, 181, 73, 1) !important;
          // background-color: rgba(0, 0, 0, 0.1) !important;
          background: rgba(247, 250, 248, 1) !important;
        }
      }

      ::v-deep .el-submenu .el-submenu__title {
        // padding: 0 40px !important;
        height: 48px !important;
        line-height: 46px !important;
        list-style: none !important;
        font-size: 16px !important;

        &:hover {
          // color: #0776f9 !important;
          // color: rgba(63, 181, 73, 1) !important;
          // color: #fff !important;
          // background-color: rgba(0, 0, 0, 0.1) !important;
          background: rgba(247, 250, 248, 1) !important;
        }
      }
    }
  }

  .el-main {
    padding: 0;
  }

  ::v-deep .vuepress-markdown-body:not(.custom) {
    padding: 0;
  }
}

// ::v-deep .el-menu-vertical-demo{
//   background: var(--transparent-color-two) !important;
// }
::v-deep .el-menu--inline {
  background: 0 !important;
}

// ::v-deep .el-submenu{
//   background: var(--transparent-color-two) !important;
// }
// ::v-deep .pl-50{
//   background: var(--transparent-color-two) !important;
// }

.el-menu-item:focus {
  background: 0 !important;
}

::v-deep .el-input__inner:hover,
::v-deep .el-input__inner:focus {
  border-color: var(--background-color) !important;
}

.el-input__inner:hover {
  border-color: var(--background-color) !important;
}

// .el-menu-item .is-active{
//   color: rgba(7, 118, 249, 1) !important;
// }
</style>
<style lang='scss'>
.el-tooltip__popper.is-green {
  background: rgba(63, 181, 73, 1);
  color: #fff;
}
.el-tooltip__popper[x-placement^='right'] .popper__arrow::after {
  border-right-color: #3fb549 !important;
}
.el-tooltip__popper[x-placement^='right'] .popper__arrow {
  border-right-color: #3fb549 !important;
}
</style>